package com.siwei.mes.job;

import com.siwei.mes.common.util.DateHelper;
import com.siwei.mes.entity.experiment.ExperimentCheckItemorder;
import com.siwei.mes.entity.experiment.ExperimentGenConfig;
import com.siwei.mes.enums.CheckEnum;
import com.siwei.mes.enums.MaterialsEnum;
import com.siwei.mes.enums.TestProjectEnum;
import com.siwei.mes.mapper.experiment.ExperimentCheckItemorderMapper;
import com.siwei.mes.mapper.experiment.ExperimentGenConfigMapper;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

/**
 * 清空抗压强度  委托有效期
 *
 * @Description: 清空抗压强度  委托有效期
 * @Version: 1.0
 * @author: mlchen
 * @date: 2024/7/23 21:35
 */
@Component
public class ClearConcreteTimeJob {
    private static final Logger log = LoggerFactory.getLogger("job");
    @Resource
    private ExperimentGenConfigMapper experimentGenConfigMapper;
    @Resource
    private ExperimentCheckItemorderMapper experimentCheckItemorderMapper;

    // 创建一个固定大小的线程池
    private final ExecutorService executorService = Executors.newFixedThreadPool(5);

    /**
     * 每小时清空一次   之前的数据占时不用做兼容了 发布版本的时候直接一次性清清除就行了  2024-07-23之前的
     */
    @Scheduled(cron = "0 0 */1 * * ?")
    public void clearJob() {
        List<TestProjectEnum> testProjectCodeList = new ArrayList<>();
        testProjectCodeList.add(TestProjectEnum.CONCRETE_PARAM_KYQD);
        testProjectCodeList.add(TestProjectEnum.CONCRETE_PARAM_KZQD);

        for (TestProjectEnum testProjectEnum : testProjectCodeList) {
            //this.clearService(testProjectEnum);
            // 提交任务到线程池
            executorService.submit(() -> clearService(testProjectEnum));
        }
    }

    private void clearService(TestProjectEnum testProjectEnum) {
        log.info("开始清理{}  委托有效期定时任务！", testProjectEnum.getName());
        ExperimentGenConfig selectConfig = new ExperimentGenConfig();
        selectConfig.setExperimentType(MaterialsEnum.CONCRETE.getCode());
        selectConfig.setCheckType(CheckEnum.BATCH.getCode());
        selectConfig.setIsCustom(1);
        selectConfig.setTestCode(testProjectEnum.getCode());

        ExperimentGenConfig genConfig = experimentGenConfigMapper.selectOne(selectConfig);
        if (genConfig == null || genConfig.getIsOpen() == 0) {
            log.info("混凝土对应生成委托规则未开启，直接跳过");
            return;
        }

        Integer checkRules = genConfig.getCheckRules();
        if (checkRules == null) {
            log.info("混凝土配置不是按重量去生成委托的，直接跳过");
            return;
        }
        if (checkRules != 2) {
            log.info("混凝土配置不是按重量去生成委托的，直接跳过");
            return;
        }

        Integer batchDay = genConfig.getBatchDay();//班次天数 多少天
        String batchTime = genConfig.getBatchTime();//班次时间  到几点
        log.info("班次天数：【{}】，班次时间：【{}】", batchDay, batchTime);
        if (batchDay == null || StringUtils.isBlank(batchTime)) {
            log.info("班次天数或班次时间为空，直接跳过");
            return;
        }
        if (batchDay == 0) {
            log.info("班次天数不允许为0");
            return;
        }

        /**这里只有混凝土的抗压强度试验*/
        List<ExperimentCheckItemorder> checkItemorderList = experimentCheckItemorderMapper.getFirstRecord(testProjectEnum.getCode());
        if (checkItemorderList.isEmpty()) {
            log.info("清空混凝土【{}】类型不存在数据", testProjectEnum.getCode());
            return;
        }

        for (ExperimentCheckItemorder checkItemorder : checkItemorderList) {
            Date erpCreateTime = checkItemorder.getErpCreateTime();//最开始记录的时间
            Long experimentId = checkItemorder.getExperimentId();

            log.info("混凝土【{}】最早记录时间：【{}】,试验委托id：【{}】", testProjectEnum.getName(), DateHelper.formatDateTime(erpCreateTime), experimentId);

            /**这里是拿最开的时间 加上 班次天数*/
            Date lastDate = DateHelper.addDate(erpCreateTime, Calendar.DATE, batchDay);
            /**这里把时间装换成 yyyy-MM-dd*/
            String dateTime = DateHelper.formatDateTime(lastDate, DateHelper.getDefaultDateFormat(), DateHelper.getDefaultLocal());
            /**添加小时和分钟*/
            String lastBatchTime = dateTime + " " + batchTime + ":00";

            log.info("清理混凝土【{}】有效期，最后有效期时间：【{}】", testProjectEnum.getName(), lastBatchTime);

            /**判断是不是今天*/
            boolean isToday = DateHelper.isToday(erpCreateTime);
            if (isToday) {
                log.info("最早的混凝土【{}】记录是当天，不清理！", testProjectEnum.getName());
                return;
            }

            /**当前时间要大于 最后批次时间*/
            if (new Date().getTime() > DateHelper.parseDateTime(lastBatchTime).getTime()) {
                /**删除这个时间之前的数据*/
                log.info("符合条件，清空【{}】之前的数据！", lastBatchTime);
                log.info("开始清空【experimentCheckItemorder】表数据，只清空【批检】【{}】，试验委托id：【{}】", testProjectEnum.getName(), experimentId);
                experimentCheckItemorderMapper.deleteByExperimentId(experimentId);
            }
        }
    }

    public static void main(String[] args) {
        Date erpCreateTime = DateHelper.parseDateTime("2025-10-27 23:58:08");
        Integer batchDay = 1;//班次天数 多少天
        String batchTime = "00:00";//班次时间  到几点
        /**这里是拿最开的时间 加上 班次天数*/
        Date lastDate = DateHelper.addDate(erpCreateTime, Calendar.DATE, batchDay);
        /**这里把时间装换成 yyyy-MM-dd*/
        String dateTime = DateHelper.formatDateTime(lastDate, DateHelper.getDefaultDateFormat(), DateHelper.getDefaultLocal());
        /**添加小时和分钟*/
        String lastBatchTime = dateTime + " " + batchTime + ":00";

        System.out.println(lastBatchTime);

        /**判断是不是今天*/
        boolean isToday = DateHelper.isToday(erpCreateTime);
        if (isToday) {
            return;
        }

        /**当前时间要大于 最后批次时间*/
        if (new Date().getTime() > DateHelper.parseDateTime(lastBatchTime).getTime()) {
            /**删除这个时间之前的数据*/
            log.info("符合条件，清空【{}】之前的数据！", lastBatchTime);
        }
    }
}
